package com.ellfors.common.base

import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.ellfors.common.R
import com.ellfors.common.ext.clickNoShadow

@Composable
fun BaseComposeTitleBar(
    activity: AppCompatActivity,
    titleBarVisible: MutableState<Boolean> = mutableStateOf(true),
    backVisible: MutableState<Boolean> = mutableStateOf(true),
    title: MutableState<String> = mutableStateOf(""),
    backListener: MutableState<(() -> Unit)?> = mutableStateOf(null)
) {
    if (!titleBarVisible.value) {
        return
    }
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(45.dp)
            .background(color = Color.White)
    ) {
        if (backVisible.value) {
            Image(
                painter = painterResource(id = R.drawable.ic_title_back),
                contentDescription = "",
                contentScale = ContentScale.Inside,
                modifier = Modifier
                    .width(45.dp)
                    .height(45.dp)
                    .align(Alignment.TopStart)
                    .clickNoShadow {
                        backListener.value?.invoke() ?: also { activity.finish() }
                    }
            )
        }
        Text(
            text = title.value,
            fontWeight = FontWeight.Bold,
            color = Color(0xFF333333),
            fontSize = 16.sp,
            modifier = Modifier.align(Alignment.Center)
        )
        Divider(
            modifier = Modifier
                .fillMaxWidth()
                .height(1.dp)
                .align(Alignment.BottomCenter),
            color = Color(0xFFEAEAEA)
        )
    }
}